package com.kstech.mp3604.fragments

import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.kstech.mp3604.R
import com.kstech.mp3604.adapter.AllTaskAdapter
import com.kstech.mp3604.adapter.GridSpaceItemDecoration
import com.kstech.mp3604.base.BaseDialogFragment
import com.kstech.mp3604.bean.StackCoordinate
import com.kstech.mp3604.bean.TaskData
import com.kstech.mp3604.data.DbManager
import com.kstech.mp3604.databinding.DialogTaskBinding
import com.kstech.mp3604.ui.listener.TaskSelectListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * 任务按钮  对话框界面
 */
class DialogTaskFragment(val listener: TaskSelectListener) :
    BaseDialogFragment<DialogTaskBinding>(DialogTaskBinding::inflate) {
    /**
     * 有一个dataList 读取数据库中的数据集
     * 有一个showList 渲染到adapter中的数据集
     * showList.size 恒大于等于 dataList
     */
    private val showList = mutableListOf<StackCoordinate>()

    override fun onInitView() {

        binding.closeDialog.setOnClickListener {
            dialog!!.cancel()
        }
        lifecycleScope.launch(Dispatchers.IO) {
            val dataList = DbManager.db.taskDataDao().queryAllTask()
            withContext(Dispatchers.Main) {
                if (dataList.size > 0) {
                    binding.emptyDataMsg.visibility = View.GONE
                    updateView(dataList)
                } else {
                    binding.emptyDataMsg.visibility = View.VISIBLE
                    binding.emptyDataMsg.text = "暂无数据"
                }
            }
        }

    }

    private fun updateView(dataList: List<TaskData>) {
        val taskAdapter = AllTaskAdapter(true)
        taskAdapter.addOnItemChildClickListener(
            R.id.receiveData
        ) { adapter, view, position ->
            dialog!!.cancel()
            //弹出确认对话框
            listener.showConfirmDialog(showList[position])
        }

        binding.recyclerView.layoutManager = LinearLayoutManager(requireActivity())
        binding.recyclerView.addItemDecoration(GridSpaceItemDecoration(10, 10))
        binding.recyclerView.adapter = taskAdapter
        showList.clear()
        /**
         * taskData.operateType 是REARRANGE倒垛
         * 这种工况不同于入库、出库  一个taskData data 可能有多个， data中的stackCoordinates 必定有两个，stackCoordinates[0]起始堆，stackCoordinates[1]码放堆
         * 也就是从堆[0]把货物搬到堆[1]
         * 当时这种工况的时候需要在存入数据库的时候做特殊处理
         */
        //储存格式转换adapter显示格式
        dataList.forEach {

//            it.stackCoordinates.forEach { c_it ->
//                val s = c_it.apply {
//                    taskData = it
//                }
//                showList.add(s)
//            }
            if (it.operateType == "REARRANGE" && it.stackCoordinates.size >= 2) {
                it.stackCoordinates[0].rearrange_x = it.stackCoordinates[1].x
                it.stackCoordinates[0].rearrange_StackNumber = it.stackCoordinates[1].stackNumber
                it.stackCoordinates[0].taskData = it
                showList.add(it.stackCoordinates[0])
            } else {
                it.stackCoordinates.forEach { c_it ->
                    val s = c_it.apply {
                        taskData = it
                    }
                    showList.add(s)
                }
            }
        }


        taskAdapter.submitList(showList)
    }
}